package com.kzd.btgame.main.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kzd.btgame.R
import com.kzd.btgame.entity.LotteryObtain
import com.module.lemlin.image.loadCenterImage
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AlphaConfig
import razerdp.util.animation.AnimationHelper


class LotteryObtPopup(context: Context) :
    BasePopupWindow(context) {

    init {
        popupGravity = Gravity.CENTER
        setOverlayNavigationBar(false)
    }


    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_lottery_obt)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)

        contentView.findViewById<ImageView>(R.id.ivLotteryObtClose)
            .setOnClickListener {
                dismiss()
            }

        contentView.findViewById<ImageView>(R.id.ivLotteryObtOk)
            .setOnClickListener {
                dismiss()
            }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.IN)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.OUT)
            .toDismiss()
    }

    fun setContent(content: MutableList<LotteryObtain>): LotteryObtPopup {
        val manager = GridLayoutManager(context,if (content.size > 4) 4 else content.size)
        contentView.findViewById<RecyclerView>(R.id.rvLotteryObtBody).apply {
            layoutManager = manager
        }.adapter = LotteryObtAdapter().apply {
            setNewInstance(content)
        }
        return this
    }

    private class LotteryObtAdapter :
        BaseQuickAdapter<LotteryObtain, BaseViewHolder>(R.layout.item_lottery_obtain) {
        override fun convert(holder: BaseViewHolder, item: LotteryObtain) {
            holder.setText(R.id.tvLotteryObtainName, item.name)
                .getView<ImageView>(R.id.ivLotteryObtainIcon)
                .loadCenterImage(src = item.icon?:R.mipmap.ic_90003)
        }
    }


}